#include "PI.h"
#include <limits.h>
#include <signal.h>
#include <float.h>

int my_atof(const char *str, double *pnumber)
{
        /* Convert char str to double data type. */
        double          retval;
        double          one_tenth = 0.1;
        double          ten = 10.0;
        double          zero = 0.0;
        int             found_digits = 0;
        int             is_negative = 0;
        char           *num;

        /* Check pointers. */
        if (str == 0) {
                *pnumber = zero;
                return 0;
        }
        retval = zero;
        
        num = new char[strlen(const_cast<const char*>(str)) + 1];
        strcpy(num, str);
        //num = str;

        /* Advance past white space. */
        while (isspace(*num))
                num++;

        /* Check for sign. */
        if (*num == '+')
                num++;
        else if (*num == '-') {
                is_negative = 1;
                num++;
        }
        /* Calculate the integer part. */
        while (isdigit(*num)) {
                found_digits = 1;
                retval *= ten;
                retval += *num - '0';
                num++;
        }

        /* Calculate the fractional part. */
        if (*num == '.') {
                double          scale = one_tenth;
                num++;
                while (isdigit(*num)) {
                        found_digits = 1;
                        retval += scale * (*num - '0');
                        num++;
                        scale *= one_tenth;
                }
        }
        /* If this is not a number, return error condition. */
        if (!found_digits) {
                *pnumber = zero;
                return 0;
        }
        /* If all digits of integer & fractional part are 0, return 0.0 */
        if (retval == zero) {
                *pnumber = zero;
                return 1;               /* 
                                         * Not an error condition, and no
                                         * need to
                                         * continue. */
        }
        /* Process the exponent (if any) */
        if ((*num == 'e') || (*num == 'E')) {
                int             neg_exponent = 0;
                int             get_out = 0;
                long            index;
                long            exponent = 0;
                double          getting_too_big = DBL_MAX * one_tenth;
                double          getting_too_small = DBL_MIN * ten;

                num++;
                if (*num == '+')
                        num++;
                else if (*num == '-') {
                        num++;
                        neg_exponent = 1;
                }
                /* What if the exponent is empty?  Return the current
                 * result. */
                if (!isdigit(*num)) {
                        if (is_negative)
                                retval = -retval;

                        *pnumber = retval;

                        return (1);
                }
                /* Convert char exponent to number <= 2 billion. */
                while (isdigit(*num) && (exponent < LONG_MAX / 10)) {
                        exponent *= 10;
                        exponent += *num - '0';
                        num++;
                }

                /* Compensate for the exponent. */
                if (neg_exponent) {
                        for (index = 1; index <= exponent && !get_out; index++)
                                if (retval < getting_too_small) {
                                        get_out = 1;
                                        retval = DBL_MIN;
                                } else
                                        retval *= one_tenth;
                } else
                        for (index = 1; index <= exponent && !get_out; index++) {
                                if (retval > getting_too_big) {
                                        get_out = 1;
                                        retval = DBL_MAX;
                                } else
                                        retval *= ten;
                        }
        }
        if (is_negative)
                retval = -retval;
        
        //cout << "tt " << retval << endl;
        *pnumber = retval;
        
        return (1);
}
/*
int get3DEdgeID(const point3d p1, const point3d p2, routingInst *rst){
  	
  	int edgeCount2D = (rst->yGrid-1)*rst->xGrid*(rst->layer/2) + (rst->xGrid-1)*rst->yGrid*(rst->layer/2);
  	int edgeCountLayer = 0;
	int layerp = (p1.z <= p2.z) ? p1.z : p2.z;
	
	edgeCountLayer = ((rst->xGrid-1)*rst->yGrid + rst->xGrid*(rst->yGrid-1))*(layerp/2);
		
	
	// horizontal edge
	if ((p1.y == p2.y) && (p1.z == p2.z))
	{
		if (p2.x - p1.x == 1)
			return p1.y*(rst->xGrid-1) + p1.x + edgeCountLayer;
		else if (p1.x - p2.x == 1)
			return p1.y*(rst->xGrid-1) + p2.x + edgeCountLayer;
		else
		{
			// error, do nothing	
		}
	}
	else if ((p1.x == p2.x) && (p1.z == p2.z))
	{
		if (p2.y - p1.y == 1)
			return rst->yGrid*(rst->xGrid-1) + p1.x*(rst->yGrid-1) + p1.y + edgeCountLayer;
		else if (p1.y - p2.y == 1)
			return rst->yGrid*(rst->xGrid-1) + p1.x*(rst->yGrid-1) + p2.y + edgeCountLayer;
		else
		{
			// error, do nothing	
		}
	}
	else if ((p1.x == p2.x) && (p1.y == p2.y))
	{
		if (p2.z - p1.z == 1)
			return edgeCount2D + (p1.x*(rst->yGrid)+p1.y)*(rst->layer-1) + p1.z ;
		else if (p1.z - p2.z == 1)
			return edgeCount2D + (p2.x*(rst->yGrid)+p2.y)*(rst->layer-1) + p2.z ;		
		else
		{
			// error, do nothing	
		}
	}
	else
	{
		// error, do nothing
	}

	
	
	fprintf (stderr, "can't get edge1 :");
	return -1;
  	
}
*/
